package com.doone.telmarket.action.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.domain.ServiceProduct;
import com.doone.telmarket.domain.ServiceProductView;
import com.doone.telmarket.domain.ibator.ServiceClassify;
import com.doone.telmarket.domain.ibator.ServiceType;
import com.doone.telmarket.service.ServiceClassifyOperation;
import com.doone.telmarket.service.ServiceOperation;
import com.doone.telmarket.service.ServiceProductOperation;
import com.doone.telmarket.service.ServiceTypeOperation;
import com.doone.telmarket.service.SiteResourceManager;
import com.doone.telmarket.vo.ServiceVo;
import com.opensymphony.xwork2.ActionContext;

public class IndexAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(ServiceTypeAction.class);

	private List<ServiceType> serviceTypeList;

	private String errorMsg;

	@Autowired
	private ServiceTypeOperation remoteServiceTypeOperation;

	@Autowired
	private ServiceProductOperation remoteServiceProductOperation;

	@Autowired
	private ServiceOperation remoteServiceOperation;

	private String serviceTypeName;

	private String serviceCode;

	private String productId;

	private String orderid;

	private String serviceClassifyName;

	@Autowired
	private SiteResourceManager siteResourceManager;

	@Autowired
	private ServiceClassifyOperation remoteServiceClassifyOperation;

	/**
	 * @description 获取所有增值业务服务类别列表
	 * @author 甘雨波
	 * @date 2009-9-15 10:13
	 * @return
	 */
	@Action(value = "toEditIndexHotBusi", results = { @Result(name = "success", location = "/admin/indexHotBusi.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String selectAllServiceType() {
		try {
			serviceTypeList = remoteServiceTypeOperation.selectAllServiceType();
			//log.info(serviceTypeList.size());
			return "success";
		} catch (Exception e) {
			log.info("获取增值业务服务类别列表失败" + e.getMessage());
			errorMsg = "获取增值业务服务类别列表失败";
			return "error";
		}

	}

	@SuppressWarnings("static-access")
	@Action("getProductByTypeName")
	public void getProductByType() {
		try {
			List<ServiceProduct> li = remoteServiceProductOperation.getProductByServiceType(serviceTypeName);
			JSONUtil js = new JSONUtil();
			String jsonString = js.serialize(li);
			responseWrite(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action("deleteHotBusi")
	public void deleteHotBusi() {
		try {
			remoteServiceProductOperation.deleteHotByServiceTypeName(serviceTypeName);
			responseWrite("删除数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("删除数据失败");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	@Action("saveIndexHotBusi")
	public void saveIndexHotBusi() {
		try {
			ServiceProductView productView = new ServiceProductView();
			productView.setServiceCode(Integer.parseInt(serviceCode));
			productView.setProductid(productId);
			productView.setOrderid(Integer.parseInt(orderid));
			remoteServiceProductOperation.saveHotBusi(productView);
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			pagePath.append("/admin/templete/newBusi.jsp");

			ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			generatePage(path, pagePath.toString());

			responseWrite("保存数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("保存数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action("getTyzzList")
	public void getTyzzList() {
		try {
			StringBuffer tb = new StringBuffer();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getServerName());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());

			List<ServiceType> typeList = remoteServiceTypeOperation.selectAllServiceType();

			for (int z = 0; z < typeList.size(); z++) {
				tb.append("<table width=\"536\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
				tb
						.append("<td height=\"24\" align=\"center\" valign=\"middle\" background=\"/telmarket-web/images/list/index_7.gif\">");
				tb.append("<table width=\"96%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
				tb.append("<td width=\"456\" align=\"left\" class=\"heise_B_12\">"
						+ typeList.get(z).getServiceTypeName() + "</td>");
				tb.append("<td width=\"72\" align=\"right\" valign=\"middle\" class=\"huise_12\"><a href=\""
						+ pagePath.toString() + "/web/getStartPage?serviceTypeName="
						+ typeList.get(z).getServiceTypeName() + "\" target=\"_parent\">查看更多&gt;&gt;</a></td>");
				tb.append("</tr></table></td></tr></table>");

				List<ServiceProductView> list = remoteServiceProductOperation.selectIndexHotBusi(typeList.get(z)
						.getServiceTypeName());
				tb
						.append("<table width=\"100%\" height=\"145\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-top:12px; margin-bottom:20px;\"><tr id=\"tyzzyw\"  align=\"center\">");
				int i = 0;
				for (; i < list.size(); i++) {
					ServiceProductView view = list.get(i);

					tb
							.append("<td width=\"25%\" align=\"center\" ><table width=\"99\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
					tb
							.append("<td height=\"75\" colspan=\"2\" align=\"center\" valign=\"middle\" style=\"border:1px solid #dfe8ef;\"><a href=\""
									+ pagePath.toString()
									+ view.getPageUrl()
									+ "\"  target=\"_parent\"><img src=\""
									+ pagePath.toString()
									+ view.getPageSmallIcon()
									+ "\" width=\"90\" height=\"65\" border=\"0\" /></a></td>");
					tb.append("</tr><tr>");
					tb.append("<td height=\"22\" colspan=\"2\" align=\"left\" valign=\"bottom\"><a href=\""
							+ pagePath.toString() + view.getPageUrl() + "\"  target=\"_parent\">"
							+ view.getProductName() + "</a></td></tr><tr>");
					tb
							.append("<td height=\"20\" colspan=\"2\" align=\"left\" valign=\"middle\" class=\"juhongse_12\">订购价格:"
									+ view.getFee() / 100 + "元/月</td>");
					tb.append("</tr><tr>");
					if ("E".equals(view.getCanAccept())) {
						tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"><img src=\""
								+ pagePath.toString() + "/images/bnt_1.gif\" onclick=\"submitForm('buyValueAdded','"
								+ view.getProductid() + "','" + view.getProductName() + "','" + view.getFee() / 100
								+ "','" + view.getOrderMode()
								+ "');\" style=\"cursor: hand\" width=\"45\" height=\"21\" border=\"0\" /></td>");
						tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"><img src=\""
								+ pagePath.toString()
								+ "/images/bnt_2.gif\"  onclick=\"submitForm('cancelValueAdded','"
								+ view.getProductid() + "','" + view.getProductName() + "','" + view.getFee() / 100
								+ "','" + view.getOrderMode()
								+ "');\" style=\"cursor: hand\"  width=\"45\" height=\"21\" border=\"0\" /></td>");
					} else {
						tb
								.append("<td width=\"110\"  align=\"left\" valign=\"middle\" class=\"juhongse_12\"><a target=\"_parent\" href=\""
										+ pagePath.toString()
										+ view.getPageUrl()
										+ "\"><img src=\""
										+ pagePath.toString()
										+ "/images/xxck.jpg\"   style=\"cursor: hand\"  height=\"21\" border=\"0\" /></a></td>");

					}

					tb.append(" </tr>");
					tb.append("</table></td>");
					
				}
				for (; i < 4; i++) {
					if (i == 0) {
						tb.append("<td>没有数据</td>");
						break;
					}
					tb
							.append("<td width=\"25%\" align=\"center\"  ><table width=\"99\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
					tb
							.append("<td height=\"75\" colspan=\"2\" align=\"center\" valign=\"middle\" style=\"border:1px solid #dfe8ef;\"></td>");
					tb.append("</tr><tr>");
					tb.append("<td height=\"22\" colspan=\"2\" align=\"left\" valign=\"bottom\"></td></tr><tr>");
					tb
							.append("<td height=\"20\" colspan=\"2\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");
					tb.append("</tr><tr>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");

					tb.append(" </tr>");
					tb.append("</table></td>");
				}
				tb.append("</tr></table>");
				
				
				if(z == (typeList.size()-1)){
					
				}

			}
			
			tb.append("<table width=\"570\" style=\"margin-left:5px\" align=\"left\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr style=\"float:left\" ><td style=\"float:left\" align=\"left\" >");
			tb.append("<iframe width=\"570\"  height=\"180\"  scrolling=\"No\" frameborder=\"0\" src=\"http://www.189store.com/appstore/infoshow/handinner/list_5.htm\"></iframe>");
			tb.append("</td></tr></table>");

			responseWrite(tb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@SuppressWarnings("static-access")
	@Action("selectServiceClassify")
	public void selectServiceClassify() {
		try {
			List<ServiceClassify> serviceClassify = remoteServiceClassifyOperation.selectAllServiceClassify();
			responseWrite(new JSONUtil().serialize(serviceClassify));
		} catch (Exception e) {
			log.info("获取增值业务功能类别列表失败" + e.getMessage());
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action("selectProduct")
	public void selectProduct() {
		try {

			List<ServiceClassify> serviceClassify = remoteServiceClassifyOperation.selectAllServiceClassify();
			StringBuffer sb = new StringBuffer();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getServerName());
			log.info(pagePath);
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());

			List<ServiceType> typeList = remoteServiceTypeOperation.selectAllServiceType();

			for (int y = 0; y < typeList.size(); y++) {
				sb
						.append("<table width=\"183\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"24\" align=\"left\" background=\""
								+ pagePath.toString()
								+ "/images/lm_04.gif\" style=\"padding-left:6px; padding-top:2px;\"><strong><a href=\""
								+ pagePath.toString()
								+ "/web/getStartPage?serviceTypeName="
								+ typeList.get(y).getServiceTypeName()
								+ "\">"
								+ typeList.get(y).getServiceTypeName()
								+ "</a></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href=\""
								+ pagePath.toString()
								+ "/web/getStartPage?serviceTypeName="
								+ typeList.get(y).getServiceTypeName() + "\">更多</a></strong></td></tr>");
				sb.append("<tr><td height=\"100\" align=\"center\" valign=\"top\">");
				
				for (int i = 0; i < serviceClassify.size(); i++) {
					ServiceVo vo = new ServiceVo();
					vo.setServiceClassifyName(serviceClassify.get(i).getServiceClassifyName());
					vo.setServiceTypeName(typeList.get(y).getServiceTypeName());

					List<ServiceVo> serviceVoList = remoteServiceOperation.queryServiceVoByTypeNameAndClassifyName(vo);

					if (serviceVoList.size() > 0) {
						sb
								.append("<table width=\"152\" height=\"14\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-top:12px; margin-bottom:4px;\">");
						sb
								.append("<tr><td width=\"152\" align=\"left\" style=\"line-height:18px; color:#ad1315;\" class=\"zhonghong\"><strong><a>"
										+ serviceClassify.get(i).getServiceClassifyName() + "</a></strong><br />");

						int xy = serviceVoList.size();
						if(xy>6){
							xy = 6;
						}
						for (int z = 0; z < xy; z++) {
							if (z % 2 != 0) {
								sb.append("&nbsp;<a href='" + pagePath.toString()
										+ serviceVoList.get(z).getServiceUrl() + "'>"
										+ serviceVoList.get(z).getServiceName() + "</a><br/>");
							} else {
								sb.append("&nbsp;&nbsp;&nbsp;<a href='" + pagePath.toString()
										+ serviceVoList.get(z).getServiceUrl() + "'>"
										+ serviceVoList.get(z).getServiceName() + "</a>&nbsp;&nbsp;|");
							}
						}
						sb.append("</td></tr></table>");
					}
				}
				sb.append("</td></tr></table>");
				
				if(y == (typeList.size()-1)){
					sb
					.append("<table width=\"183\" border=\"0\"  cellspacing=\"0\" cellpadding=\"0\"><tr><td >");
					sb.append("<iframe width=\"183\"  height=\"258\"  scrolling=\"No\" frameborder=\"0\"src=\"http://www.189store.com/appstore/infoshow/handinner/left_tianyi.html\"></iframe>");
					sb.append("</td></tr></table>");
				}
			}
			
			if(typeList.size()==0){
				sb
				.append("<table width=\"183\" border=\"0\"  cellspacing=\"0\" cellpadding=\"0\"><tr><td >");
				sb.append("<iframe width=\"183\"  height=\"258\"  scrolling=\"No\" frameborder=\"0\"src=\"http://www.ctmarket.cn/appstore/infoshow/handinner/left_tianyi.html\"></iframe>");
				sb.append("</td></tr></table>");
			}
			
			sb
					.append("<table width=\"183\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><img src=\"/telmarket-web/images/lm_06.gif\" width=\"185\" height=\"6\" /></td></tr></table>");
			
			responseWrite(sb.toString());
		} catch (Exception e) {
			log.info("获取增值业务功能类别列表失败" + e.getMessage());
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action("generateIndexPage")
	public void generateIndexPage() {
		try {
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			pagePath.append("/admin/index.jsp");

			ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");

			log.info(path);
			log.info(pagePath.toString());

			siteResourceManager.writePage(pagePath.toString(), path + "/web/index.htm");
			responseWrite("生成首页成功");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("生成首页失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}

	private void generatePage(String rootPath, String pagePath) throws IOException {
		siteResourceManager.writePage(pagePath, rootPath + "/web/common/newBusi.html");
	}

	public List<ServiceType> getServiceTypeList() {
		return serviceTypeList;
	}

	public void setServiceTypeList(List<ServiceType> serviceTypeList) {
		this.serviceTypeList = serviceTypeList;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getServiceTypeName() {
		return serviceTypeName;
	}

	public void setServiceTypeName(String serviceTypeName) {
		this.serviceTypeName = serviceTypeName;
	}

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getServiceClassifyName() {
		return serviceClassifyName;
	}

	public void setServiceClassifyName(String serviceClassifyName) {
		this.serviceClassifyName = serviceClassifyName;
	}

}
